package com.secondary.dao.administrator.impl;

import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Component;

import com.secondary.dao.BaseDao;
import com.secondary.dao.administrator.AdministratorDao;
import com.secondary.pojos.Administrator;
import com.secondary.pojos.Goods;
import com.secondary.pojos.User;
@Component("adminDao")
public class AdministratorDaoImpl extends BaseDao implements AdministratorDao{

	@Override
	public Administrator login(String name, String passWord) {
		Administrator administrator = new Administrator();
		administrator  = (Administrator) findObjectByProperty(Administrator.class, "username", name);
			
		if (administrator != null &&administrator.getPassWord().equals(passWord)) {
			return administrator;
		} else {
			return null;

		}
	}

	@Override
	public List<Goods> findUncheckedGoods() {
		List<Goods> list=getSession().createQuery("select new Goods(g.id,g.name,g.state,g.startTime,g.postDays) " +
				"from Goods g where g.state=0 ").list();
		return list;
	}

	@Override
	public List<User> findAllUser() {
		return getSession().createQuery("from User").list();
	}

	@Override
	public void updateGoodState(int state,int id) {
		getSession().createQuery("update Goods g set g.state=:state where id=:id").setParameter("state", state).setParameter("id", id).executeUpdate();
		
	}

	@Override
	public void updateUserState(int state,int id) {
		getSession().createQuery("update User u set u.state=:state where id=:id").setParameter("state", state).setParameter("id", id).executeUpdate();
	}
	
}
